package middleware

import (
	"github.com/gin-gonic/gin"

	v1 "torler.com/qingyue/apps/backend/internal/transport/http/v1"
)

// GlobalHeaderRequireMiddleware is a middleware that checks if the request has the required headers.
// If the headers are not present, it returns an error response and aborts the request.
func GlobalHeaderRequireMiddleware() gin.HandlerFunc {
	return func(ctx *gin.Context) {
		req := new(v1.HeaderReq)
		if err := ctx.ShouldBindHeader(&req); err != nil {
			v1.NewRsp(ctx).WithMsg(v1.ErrHeaderInvalid).Send()
			ctx.Abort()
			return

		}
		ctx.Set("header", req)
		ctx.Next()
	}
}
